package gateway.mgt.api

import gateway.mgt.runtime.GatewayCore
import groovy.transform.ToString

//import grails.rest.Resource
//@Resource(uri = "/apiGroup")
@ToString(includePackage = false, includes = ["name"])
class ApiGroup {

    String name

    Date dateCreated
    Date lastUpdated

    SortedSet apis

    static hasMany = [apis: ApiDefinition]
    static belongsTo = [core: GatewayCore]

    static constraints = {
        name(blank: false, unique: true)
        core(nullable: true)
        dateCreated()
        lastUpdated()
    }

    def beforeValidate() {
        if (!core) {
            core = GatewayCore.findByName(name)

            if (!core)
                core = new GatewayCore(name: name, description: "Default creation by ApiGroup").save()
        }
    }

    Map<String, Object> publish() {
        return core.publish()
    }
}
